<%@page import="model.UserAccount"%>
<%@page import="model.Employee"%>
<%@page import="validator.UserAccountValidator"%>    
<%@page import="java.util.ArrayList"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Locale"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<% 
	String selString = "selected = \"selected\""; 
	SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMM dd, yyyy", Locale.ENGLISH);	
	UserAccountValidator validator = (UserAccountValidator)request.getAttribute("validator");
	
	String error = "class=\"error\"", idError = "", subjectError ="", statusError ="", validFromError="", validToError="", expiresError="";
	try{
		idError = validator.getId() == null ? error : "";
		subjectError = validator.getSubject() == null ? error : "";
		statusError = validator.getStatus() == null ? error : "";
		validFromError = validator.getValidFrom() == null ? error : "";
		validToError = validator.getValidTo() == null ? error : "";
		expiresError = validator.getPasswordNeverExpires() == null ? error : "";
	} catch(NullPointerException e){ }
	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link type="text/css" rel="stylesheet" href="./jsDatePick_ltr.min.css" />
<link type="text/css" rel="stylesheet" href="./css/style.css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="./jsDatePick.jquery.min.1.3.js"></script>
</head>
<body>
	<script type="text/javascript">
		window.onload = function(){	
			new JsDatePick({ useMode:2, target:"valid_from",	dateFormat:"%F %d, %Y" });
			new JsDatePick({ useMode:2, target:"valid_to",	dateFormat:"%F %d, %Y" });
		};
		
		//UPDATE USER
	    $(function() {  
	        $("#edit_user").click(function() {	        	
	        	$.ajax({
	        		  type: 'POST',
	        		  url: './user',
	        		  data: $('#user_edit_form').serialize(),
	        		  success : function(data){
	        			  message(data.message, data.classType);
	        		  },
	        		  dataType: 'json'
	        	});	        	
	        });    
	    });
		
		function message(string, classType){
			$("#message").hide();
	    	$("#message").fadeIn();
			$("#message").text(string);
			$("#message").removeClass();
			$("#message").addClass(classType);
		}
	</script>
		
	<a href="./user">Users</a>
	<p class="error" id="message"></p>
	<% UserAccount user = (UserAccount)request.getAttribute("user"); %>
	<% ArrayList<Employee> employees = (ArrayList<Employee>)request.getAttribute("employees"); %>
	<h1>User <% out.println(user.getUsername()); %></h1>
	<form name="user_edit_form" id ="user_edit_form" action="#">
	<input type ="hidden" name ="id" id = "id" value=<% out.println(user.getId()); %>/>	
		<table>		
		<tr><td>ID: <td><td <% out.print(idError); %>><% out.println(user.getId()); %></td></tr>		
		<tr><td>Account owner: <td>
			<td>
			<select <% out.print(subjectError); %> name = "employee">
				<%
				for(Employee e: employees){
			  		out.println("<option value=\""+e.getId()+"\" "
						+(user.getSubject() == e.getId() ? selString : "")+
						">"+e.getPersonObject()+"("+e.getEnterpriseObject().getName()+")</option>");
				}
			  	%>
			</select> 
			</td>			
		</tr>
		<tr><td>Created: <td><td><% out.println(user.getCreated()); %></td></tr>
		<tr><td>Creator: <td><td><% out.println(user.getCreaterEmployee() == null ? null : user.getCreaterEmployee().getPersonObject()); %></td></tr>
		<tr><td>Status: <td>
			<td>
				<select <% out.print(statusError); %> name = "status">
					<option value = 0 <% if(user.getStatus() == 0) out.println(selString); %>>NOT OK</option>
					<option value = 1 <% if(user.getStatus() == 1) out.println(selString); %>>OK</option>
				</select>				
			</td>
		</tr>
		<tr><td>Valid from: <td>
			<td>
				<input type="text" <% out.print(validFromError); %> name="valid_from" id="valid_from" 
					value ="<% out.print(user.getValidFrom() == null ? null : dateFormat.format(user.getValidFrom())); %>"
				/>	
			</td>			
		</tr>		
		<tr><td>Valid to: <td>
			<td>
				<input type="text" <% out.print(validToError); %> name="valid_to" id="valid_to" 
					value ="<% out.print(user.getValidTo() == null ? null : dateFormat.format(user.getValidTo())); %>"/>			
			</td>			
		</tr>		
		<tr><td>Password never expires: <td>
			<td>
				<select <% out.print(expiresError); %> name = "password_never_expires">
					<option value = 0 <% if(!user.isPasswordNeverExpires()) out.print(selString); %>>No</option>
					<option value = 1 <% if(user.isPasswordNeverExpires()) out.print(selString); %>>Yes</option>
				</select>				
			</td>			
		</tr>		
		<tr>
			<td><input type="button" id="edit_user" value="Save" /></td><td></td>
		</tr>		
	</table>		
	</form>
</body>
</html>